package com.ibiaoke.system.service;

import com.ibiaoke.common.response.Res;
import com.qiniu.util.Auth;
import com.qiniu.util.StringMap;
import org.springframework.stereotype.Service;

/**
 * Created by huhaichao on 2017/6/7.
 */

@Service
public class QiNiuServiceImpl implements QiNiuService {

    public static final String RETURN_BODY = "returnBody";
    public static final String RETURN_BODY_VALUE = "{\"key\":\"$(key)\",\"hash\":\"$(etag)\",\"bucket\":\"$(bucket)\",\"fsize\":$(fsize)}";
    public static long EXPIRE_SECONDS = 36000;


    @Override
    public Res getToken(String accessKey, String secretKey, String bucketName) {
        Res res = Res.create();
        Auth auth = Auth.create(accessKey, secretKey);
        StringMap putPolicy = new StringMap();
        putPolicy.put(RETURN_BODY, RETURN_BODY_VALUE);
        String upToken = auth.uploadToken(bucketName, null, EXPIRE_SECONDS, putPolicy);
        System.out.println(upToken);
        res.addResponse("upToken",upToken);
        return res;
    }
}
